Skip to content

audio: ffmpeg_dec: add ALAC decoder and FLAC/ALAC encoder support - #11427

Merged
lgirdwood merged 5 commits into
thesofproject:mainfrom
lgirdwood:topic/ffmpeg-lossless
Nov 15, 2026
Merged

lgirdwood merged 5 commits into
thesofproject:mainfrom
lgirdwood:topic/ffmpeg-lossless

Conversation

@lgirdwood

Copy link
Copy Markdown
Member

Add Phase 1 lossless compress playback and capture codecs to ffmpeg_dec using native fixed-point libavcodec implementations:

  • ALAC decoder (CONFIG_FFMPEG_DEC_ALAC, SND_AUDIOCODEC_ALAC):
    • Apple .caf container parser (kuki chunk 36-byte ALAC magic cookie extraction and data chunk framing) and inline 36-byte QuickTime 'alac' atom header support.
    • Self-delimiting unparsed frame consumption tracking via GetBitContext and partial-frame reassembly across LL period boundaries.
  • FLAC encoder (CONFIG_FFMPEG_ENC_FLAC, SND_AUDIOCODEC_FLAC):
    • Native libavcodec flac encoder configured with fixed integer LPC predictors (FF_LPC_TYPE_FIXED, compression_level = 1) for deterministic low-MCPS DSP encoding.
    • Prepends the 42-byte "fLaC" + STREAMINFO metadata block from avctx->extradata to produce a self-contained .flac stream.
    • Bounds FlacEncodeContext dimensions on a build-tree copy of flacenc.c (FLAC_MAX_BLOCKSIZE=1152, rc_sums[1], FLAC_MAX_CHANNELS=2), reducing context allocation 122x from 7.35 MiB to 43.5 KiB.
  • ALAC encoder (CONFIG_FFMPEG_ENC_ALAC, SND_AUDIOCODEC_ALAC):
    • Native libavcodec alac encoder configured with fixed LPC prediction (compression_level = 1) and planar S16 input conversion.
    • Prepends the 36-byte QuickTime 'alac' magic cookie from avctx->extradata.
  • Topologies & Base FW Capabilities:
    • Advertise SND_AUDIOCODEC_ALAC playback and SND_AUDIOCODEC_FLAC / SND_AUDIOCODEC_ALAC capture in IPC4_SOF_CODEC_INFO.
    • Add sof-hda-generic-ffmpeg-alac-compr, sof-hda-generic-ffmpeg-flac-enc-compr, and sof-hda-generic-ffmpeg-alac-enc-compr development topology targets.

Hardware Verification (Aphid PTL ACE 3.0 & Spider TGL CAVS 2.5)

Verified end-to-end on both Aphid (ptl) and Spider (tgl):

  • ALAC .caf playback (comprC0D50, codec_id=15)
  • FLAC capture encode (comprC0D51, codec_id=10) + round-trip playback (comprC0D50)
  • ALAC capture encode (comprC0D51, codec_id=15) + round-trip playback (comprC0D50)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@lgirdwood
lgirdwood requested a balanced review from Copilot November 13, 2026 17:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/audio/ffmpeg_dec/ffmpeg.cmake Resolved
Comment thread src/audio/ffmpeg_dec/ffmpeg_dec-alac.c Resolved
Comment thread src/audio/ffmpeg_dec/ffmpeg-enc-alac.c Resolved
Comment thread src/audio/ffmpeg_dec/ffmpeg-enc-flac.c Resolved
Comment thread src/audio/ffmpeg_dec/ffmpeg_dec-alac.c Resolved
Comment thread src/audio/ffmpeg_dec/ffmpeg_dec-alac.c Resolved
Comment thread src/kernel/audio/base_fw.c Resolved
Comment thread src/audio/ffmpeg_dec/ffmpeg_dec-alac.c Resolved
Update get_codec_info() comments for FLAC, ALAC, SBC/mSBC, G.722, and
aptX compress capture entries to reference ffmpeg_enc instead of
ffmpeg_dec.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Consolidate sample-format conversion to Q1.31 using sign_extend_s24(),
Q_SHIFT(), and Q_CONVERT_QTOD() via sof_pcm_sample_to_q31(), and remove
the duplicate S24/S32-to-S16 conversion helpers from ffmpeg-encode.c.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
- Add configure_avctx() and is_trailing_eof() hooks to struct ffmpeg_dec_codec_ops
  and move G.722/aptX configuration out of core ffmpeg_dec-ffmpeg.c.

- Always stage unparsed payloads into ff->pktbuf and zero-pad
  AV_INPUT_BUFFER_PADDING_SIZE even when prev_len == 0.

- Preserve and restore av_log_get_level() across unparsed frame decode calls.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Comment thread src/audio/ffmpeg_dec/ffmpeg_dec-alac.c Resolved
- Peek ALAC_HEADER_TAG_SIZE (4 bytes) first into cd->in_buf, requiring
  8 bytes only for CAF/'alac' header detection.

- Replace container/cookie magic numbers with named ALAC_* defines.

- Pin libavcodec version (61.19.101) with _Static_assert and validate
  gb_buffer/gb_buffer_end bounds in ffmpeg_dec_alac_frame_bytes_consumed().

- Implement configure_avctx() and is_trailing_eof() hooks for ALAC.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
- Restrict FLAC_MAX_BLOCKSIZE sed substitutions in ffmpeg.cmake so the
  #define FLAC_MAX_BLOCKSIZE identifier is not overwritten.

- Check ffmpeg_dec_pcm_to_frame() return values and propagate errors in
  the FLAC, ALAC, SBC, G.722, and aptX encoders.

Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
@lgirdwood
lgirdwood force-pushed the topic/ffmpeg-lossless branch from 10764ee to 5321949 Compare November 14, 2026 19:09
@lgirdwood
lgirdwood marked this pull request as ready for review November 14, 2026 19:48
@intel-sofci

Copy link
Copy Markdown

PR 11427: test results

Run date: 2026-11-14 20:57 UTC

Tested commit: 53219497139d1215adbc8bc3e84e692092eaabbe

mtl pass rate lnl pass rate ptl pass rate wcl pass rate nvl pass rate

@lgirdwood

Copy link
Copy Markdown
Member Author

CI failure unrelated.

@lgirdwood
lgirdwood merged commit 827dc01 into thesofproject:main Nov 15, 2026
44 of 45 checks passed
@lgirdwood
lgirdwood deleted the topic/ffmpeg-lossless branch November 15, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants